<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item label="风险单元" prop="unitId">
        <el-input
          v-model="queryParams.unitId"
          placeholder="请输入风险单元"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="危险有害元素" prop="hazardousElement">
        <el-input
          v-model="queryParams.hazardousElement"
          placeholder="请输入危险有害元素"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="事故原因" prop="accidentCause">
        <el-input
          v-model="queryParams.accidentCause"
          placeholder="请输入事故原因"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="造成后果" prop="consequence">
        <el-input
          v-model="queryParams.consequence"
          placeholder="请输入造成后果"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="影响范围" prop="impactScope">
        <el-input
          v-model="queryParams.impactScope"
          placeholder="请输入影响范围"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="L值" prop="lValue">
        <el-input
          v-model="queryParams.lValue"
          placeholder="请输入L值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="E值" prop="eValue">
        <el-input
          v-model="queryParams.eValue"
          placeholder="请输入E值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="C值" prop="cValue">
        <el-input
          v-model="queryParams.cValue"
          placeholder="请输入C值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="风险等级" prop="riskLevel">
        <el-input
          v-model="queryParams.riskLevel"
          placeholder="请输入风险等级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评估时间" prop="assessmentTime">
        <el-date-picker
          clearable
          v-model="queryParams.assessmentTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择评估时间"
        >
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['app:unit:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['app:unit:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['app:unit:remove']"
          >删除</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:identification:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="identificationList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="风险辨识id" align="center" prop="identificationId" /> -->
      <el-table-column label="风险单元" align="center" prop="unitName" />

      <el-table-column
        label="辨识类型"
        align="center"
        prop="identificationType"
      >
        <template slot-scope="scope">
          <span>{{
            scope.identificationType == 1 ? "企业辨识" : "专业机构辨识"
          }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="风险单元id" align="center" prop="unitId" /> -->
      <el-table-column
        label="危险有害元素"
        align="center"
        prop="hazardousElement"
      />
      <!-- <el-table-column label="事故原因" align="center" prop="accidentCause" />
      <el-table-column label="造成后果" align="center" prop="consequence" />
      <el-table-column label="影响范围" align="center" prop="impactScope" /> -->
      <!-- <el-table-column label="L值" align="center" prop="lValue" />
      <el-table-column label="E值" align="center" prop="eValue" />
      <el-table-column label="C值" align="center" prop="cValue" /> -->
      <!-- <el-table-column label="风险等级" align="center" prop="riskLevel" /> -->
      <!-- <el-table-column label="评估时间" align="center" prop="assessmentTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.assessmentTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="评分状态" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.riskLevel?'' : 'warning'">{{ scope.row.riskLevel?'已评分' : '未评分' }}</el-tag>
          <!-- <el-tag type="warning">未评分</el-tag> -->
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['app:unit:edit']"
            >风险评级</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['app:unit:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改危险辨识对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="identrules" label-width="180px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="风险单元" prop="unitId">
              <!-- <el-input v-model="form.unitId" placeholder="请输入风险单元id" /> -->
              <el-select
                v-model="form.unitId"
                placeholder="请选择风险单元"
                :disabled="dis"
              >
                <el-option
                  v-for="item in unitOptions"
                  :key="item.unitId"
                  :label="item.unitName"
                  :value="item.unitId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="辨识类型" prop="identificationType">
              <!-- <el-input v-model="form.unitId" placeholder="请输入风险辨识类型" /> -->
              <el-select
                v-model="form.identificationType"
                placeholder="请选择辨识类型"
                :disabled="dis"
              >
                <el-option label="企业辨识" value="1"></el-option>
                <el-option label="专业机构辨识" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="危险有害元素" prop="hazardousElement">
          <el-input
            v-model="form.hazardousElement"
            placeholder="请输入危险有害元素"
            :disabled="dis"
          />
        </el-form-item>
        <template v-if="dis">
          <el-form-item label="事故类型" prop="accidentType">
            <!-- <el-input
              v-model="form.accidentType"
              placeholder="请输入事故类型"
            /> -->
            <el-select
                v-model="form.accidentType"
                placeholder="请选择风险单元"
                multiple
              >
                <el-option label="火灾" value="火灾"></el-option>
                <el-option label="其它爆炸" value="其它爆炸"></el-option>
                <el-option label="透水" value="透水"></el-option>
                <el-option label="中毒和窒息" value="中毒和窒息"></el-option>
                <el-option label="放炮" value="放炮"></el-option>
                <el-option label="冒顶片帮" value="冒顶片帮"></el-option>
                <el-option label="人身伤害" value="人身伤害"></el-option>
                <el-option label="打击致伤" value="打击致伤"></el-option>
                <el-option label="挤压致伤亡" value="挤压致伤亡"></el-option>
                <el-option label="物体打击" value="物体打击"></el-option>
                <el-option label="车辆伤害" value="车辆伤害"></el-option>
                <el-option label="其它伤害" value="其它伤害"></el-option>

                <el-option label="容器爆炸" value="容器爆炸"></el-option>
                <el-option label="高处坠落" value="高处坠落"></el-option>
                <el-option label="机械伤害" value="机械伤害"></el-option>
                <el-option label="触电" value="触电"></el-option>
                <el-option label="起重伤害" value="起重伤害"></el-option>
                <el-option label="坍塌" value="坍塌"></el-option>

                <el-option label="灼烫" value="灼烫"></el-option>
                <el-option label="更换筛网人员坠落" value="更换筛网人员坠落"></el-option>
                <el-option label="滑跌" value="滑跌"></el-option>
                <el-option label="辐射伤害" value="辐射伤害"></el-option>
                <el-option label="设备损坏" value="设备损坏"></el-option>
                <el-option label="皮带挤压致人伤亡" value="皮带挤压致人伤亡"></el-option>

                <el-option label="慢性矽肺病" value="慢性矽肺病"></el-option>
                <el-option label="电焊机弧光灼伤" value="电焊机弧光灼伤"></el-option>
                <el-option label="气焊遇明火产生爆炸" value="气焊遇明火产生爆炸"></el-option>
                <el-option label="起重行车致人伤亡" value="起重行车致人伤亡"></el-option>
                <el-option label="砂轮机伤人" value="砂轮机伤人"></el-option>
                <el-option label="切割机伤人" value="切割机伤人"></el-option>
              </el-select>
          </el-form-item>
          <el-form-item label="事故原因" prop="accidentCause">
            <el-input
              v-model="form.accidentCause"
              placeholder="请输入事故原因"
            />
          </el-form-item>
          <el-form-item label="造成后果" prop="consequence">
            <el-input v-model="form.consequence" placeholder="请输入造成后果" />
          </el-form-item>
          <el-form-item label="影响范围" prop="impactScope">
            <el-input v-model="form.impactScope" placeholder="请输入影响范围" />
          </el-form-item>
          <el-row>
            <el-col :span="12">
              <el-form-item label="L值(事故发生的可能性)" prop="lValue">
                <!-- <el-input v-model="form.lValue" placeholder="请输入L值" /> -->
                <el-select
                  v-model="form.lValue"
                  placeholder="请选择风险发生的可能性"
                  
                >
                  <el-option label="10 完全可以预料" :value="10"></el-option>
                  <el-option label="6 相当可能" :value="6"></el-option>
                  <el-option label="3 可能，但不经常" :value="3"></el-option>
                  <el-option label="1 可能性小,完全意外" :value="1"></el-option>
                  <el-option
                    label="0.5 很不可能，可以设想"
                    :value="0.5"
                  ></el-option>
                  <el-option label="0.2 极不能" :value="0.2"></el-option>
                  <el-option label="0.1 实际不能" :value="0.1"></el-option>
                  <el-option label="未评价" :value="0"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="E值(频繁程度)" prop="eValue">
                <!-- <el-input v-model="form.eValue" placeholder="请输入E值" /> -->
                <el-select
                  v-model="form.eValue"
                  placeholder="请选择风险频繁程度"
                
                >
                <el-option label="未评价" :value="0"></el-option>
                  <el-option label="10 连续暴露" :value="10"></el-option>
                  <el-option label="6 每天工作时间内暴露" :value="6"></el-option>
                  <el-option
                    label="3 每周一次，或偶尔暴露"
                    :value="3"
                  ></el-option>
                  <el-option label="2 每月一次暴露" :value="2"></el-option>
                  <el-option label="1 每年几次暴露" :value="1"></el-option>
                  <el-option label="0.5 非常罕见的暴露" :value="0.5"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="C值(后果严重性)" prop="cValue">
                <!-- <el-input v-model="form.cValue" placeholder="请输入C值" /> -->
                <el-select
                  v-model="form.cValue"
                  placeholder="请选择风险后果"
                
                >
                <el-option label="未评价" :value="0"></el-option>
                  <el-option label="100 十人以上死亡" :value="100"></el-option>
                  <el-option
                    label="40 数人死亡或造成职业病"
                    :value="40"
                  ></el-option>
                  <el-option label="15 一人死亡或急性中毒(15)" :value="15"></el-option>
                  <el-option label="7 严重伤残或职业伤害" :value="7"></el-option>
                  <el-option label="3 轻伤，需救护" :value="3"></el-option>
                  <el-option label="1轻微伤害" :value="1"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <!-- <el-col :span="12">
              <el-form-item label="风险等级" prop="riskLevel">
                <el-select
                  v-model="form.riskLevel"
                  placeholder="请选择风险单元"
                >
                  <el-option label="高度危险" value="高度危险"></el-option>
                  <el-option label="高度危险" value="高度危险"></el-option>
                  <el-option label="高度危险" value="高度危险"></el-option>
                  <el-option label="高度危险" value="高度危险"></el-option>
                </el-select>
              </el-form-item>
            </el-col> -->
          </el-row>

          <!-- <el-form-item label="评估时间" prop="assessmentTime">
            <el-date-picker
              clearable
              v-model="form.assessmentTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择评估时间"
            >
            </el-date-picker>
          </el-form-item> -->
        </template>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listIdentification,
  getIdentification,
  delIdentification,
  addIdentification,
  updateIdentification,
} from "@/api/system/identification";
import { listUnit } from "@/api/system/unit";
export default {
  name: "Identification",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      dis: true,
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 危险辨识表格数据
      identificationList: [],
      // 弹出层标题
      title: "",
      // 风险单元
      unitOptions: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        identificationType: null,
        unitId: null,
        hazardousElement: null,
        accidentType: null,
        accidentCause: null,
        consequence: null,
        impactScope: null,
        lValue: null,
        eValue: null,
        cValue: null,
        riskLevel: null,
        assessmentTime: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      identrules: {
        unitId: [{ required: true, message: "请选择风险单元", trigger: "change" }],
        identificationType: [{ required: true, message: "辨识类型", trigger: "change" }],
      },
    };
  },
  created() {
    this.getList();

    this.getUnit();
  },
  methods: {
    /** 查询危险辨识列表 */
    getList() {
      this.loading = true;
      listIdentification(this.queryParams).then((response) => {
        this.identificationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getUnit() {
      listUnit().then((response) => {
        this.unitOptions = response.rows;
        // const data = { unitId: 0, parentId: "主节点", children: [] };
        // data.children = this.handleTree(response.rows, "unitId", "unitName");
        // this.unitOptions.push(data);
        console.log(this.unitOptions);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        identificationId: null,
        identificationType: null,
        unitId: null,
        hazardousElement: null,
        accidentType: null,
        accidentCause: null,
        consequence: null,
        impactScope: null,
        lValue: null,
        eValue: null,
        cValue: null,
        riskLevel: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        assessmentTime: null,
        status: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.identificationId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.dis = false;
      this.open = true;
      this.title = "添加危险辨识";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.dis = true;
      const identificationId = row.identificationId || this.ids;
      getIdentification(identificationId).then((response) => {
        // console.log(response,'222222222222');
        this.form.identificationId = response.data.identificationId;
        this.form.unitId = response.data.unitId;
        this.form.identificationType = response.data.identificationType;
        this.form.hazardousElement = response.data.hazardousElement;
        this.open = true;
        this.title = "危险评级";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.identificationId != null) {
            this.form.accidentType = JSON.stringify(this.form.accidentType)
            updateIdentification(this.form).then((response) => {
              this.$modal.msgSuccess("评级成功");
              this.open = false;
              this.getList();
            });
          } else {
            addIdentification(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const identificationIds = row.identificationId || this.ids;
      this.$modal
        .confirm(
          '是否确认删除危险辨识编号为"' + identificationIds + '"的数据项？'
        )
        .then(function () {
          return delIdentification(identificationIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/identification/export",
        {
          ...this.queryParams,
        },
        `identification_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
